class LocationResult {
  final bool isSuccess;
  final String? error;
  final double? latitude;
  final double? longitude;
  final String? city;
  final String? country;

  const LocationResult._({
    required this.isSuccess,
    this.error,
    this.latitude,
    this.longitude,
    this.city,
    this.country,
  });

  factory LocationResult.success({
    required double latitude,
    required double longitude,
    String? city,
    String? country,
  }) {
    return LocationResult._(
      isSuccess: true,
      latitude: latitude,
      longitude: longitude,
      city: city,
      country: country,
    );
  }

  factory LocationResult.error(String error) {
    return LocationResult._(isSuccess: false, error: error);
  }

  bool get hasCoordinates => latitude != null && longitude != null;

  String get displayLocation {
    if (city != null && country != null) {
      return '$city, $country';
    } else if (city != null) {
      return city!;
    } else if (country != null) {
      return country!;
    }
    return '未知位置';
  }

  @override
  String toString() {
    if (isSuccess) {
      return 'LocationResult(lat: $latitude, lng: $longitude, location: $displayLocation)';
    } else {
      return 'LocationResult(error: $error)';
    }
  }
}
